import {routerRedux} from 'dva/router';
import api from '../api';


export default {
	namespace : 'dailysummary',
	state : {
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({ pathname, query }) => {
				// 没有参数时，向后端请求数据，并且改变query
				if( pathname === '/tradeAnalysis/tca/dailySummary' ){
					dispatch({type: 'whetherRequest',payload:query})
				}
			})
		}
	},
	effects : {

		* whetherRequest({
				payload
			}, {call, put,select}) {
				const state=yield select(state => state.dailysummary)
				// 看本次的参数和上次的参数是否一致：
				// 一致的话，不请求，不一致的话，正常请求
				// 定义一个对象用于定义参数
				const newObj={}
				for(var key in payload){
					newObj[key]=state[key]
				}
				if(JSON.stringify(payload)!=JSON.stringify(newObj)){
					yield put({
						type:'requestResult',
						payload
					})
				}

		},
		* requestResult({payload}, {call, put,select}){
			const response = yield call(api.tca.getDailysummary, payload);
			yield put({type:'clearState'})
			yield put({type: 'querySuccess', payload: response});
		},
		* changeRouter({payload}, {put}){
			yield put(routerRedux.push(payload))
		}

	},
	reducers : {
		clearState(state) {
			return{}
		},
		setState(state,{payload}) {
			return{
				state,
				payload
			}
		},
		querySuccess(state, {payload}) {
			return {
				...state,
				...payload.data
			}
		}
	}
};
